﻿
using System;
using System.Linq;
using System.Threading.Tasks;

namespace Magic.Redis;


/// <summary>
/// 提供Redis客户端操作类（基于CsRedisCore实现）
/// </summary>
public sealed partial class CsRedisCoreClient : IRedisClient
{
	/// <summary>
	/// SortedSetAdd.
	/// </summary>
	/// <typeparam name="T"></typeparam>
	/// <param name="key"></param>
	/// <param name="value"></param>
	/// <param name="score"></param>
	/// <returns></returns>
	public bool SortedSetAdd<T>(string key, T value, decimal score)
	{
		try
		{
			RedisHelper.ZAdd(key, (score, value));
			return true;
		}
		catch (Exception ex)
		{
			Faild($"An exception occurs on SortedSetAdd.key={key}&value={Serialize(value)}&score={score}", ex);
			return default;
		}
	}

	/// <summary>
	/// SortedSetAddAsync.
	/// </summary>
	/// <typeparam name="T"></typeparam>
	/// <param name="key"></param>
	/// <param name="value"></param>
	/// <param name="score"></param>
	/// <returns></returns>
	public async Task<bool> SortedSetAddAsync<T>(string key, T value, decimal score)
	{
		try
		{
			await RedisHelper.ZAddAsync(key, (score, value));
			return true;
		}
		catch (Exception ex)
		{
			Faild($"An exception occurs on SortedSetAddAsync.key={key}&value={Serialize(value)}&score={score}", ex);
			return default;
		}
	}

	/// <summary>
	/// SortedSetPop.
	/// </summary>
	/// <typeparam name="T"></typeparam>
	/// <param name="key"></param>
	/// <returns></returns>
	public T SortedSetPopMin<T>(string key)
	{
		try
		{
			var data = RedisHelper.ZPopMin<T>(key, 1);
			return data != null && data.Any() ? data[0].member : default;
		}
		catch (Exception ex)
		{
			Faild($"An exception occurs on SortedSetPopMin.key={key}", ex);
			return default;
		}
	}

	/// <summary>
	/// SortedSetPopAsync.
	/// </summary>
	/// <typeparam name="T"></typeparam>
	/// <param name="key"></param>
	/// <returns></returns>
	public async Task<T> SortedSetPopMinAsync<T>(string key)
	{
		try
		{
			var data = await RedisHelper.ZPopMinAsync<T>(key, 1);
			return data != null && data.Any() ? data[0].member : default;
		}
		catch (Exception ex)
		{
			Faild($"An exception occurs on SortedSetPopMinAsync.key={key}", ex);
			return default;
		}
	}

	/// <summary>
	/// SortedSetRemove.
	/// </summary>
	/// <typeparam name="T"></typeparam>
	/// <param name="key"></param>
	/// <param name="field"></param>
	/// <returns></returns>
	public bool SortedSetRemove<T>(string key, T field)
	{
		try
		{
			RedisHelper.ZRem(key, field);
			return true;
		}
		catch (Exception ex)
		{
			Faild($"An exception occurs on SortedSetRemove.key={key}&field={Serialize(field)}", ex);
			return default;
		}
	}

	/// <summary>
	/// SortedSetRemoveAsync.
	/// </summary>
	/// <typeparam name="T"></typeparam>
	/// <param name="key"></param>
	/// <param name="field"></param>
	/// <returns></returns>
	public async Task<bool> SortedSetRemoveAsync<T>(string key, T field)
	{
		try
		{
			await RedisHelper.ZRemAsync(key, field);
			return true;
		}
		catch (Exception ex)
		{
			Faild($"An exception occurs on SortedSetRemoveAsync.key={key}&field={Serialize(field)}", ex);
			return default;
		}
	}
}
